package com.zyx.xml.dom;

import javax.xml.parsers.*;
import java.io.*;

import cn.hutool.core.io.resource.ClassPathResource;
import org.w3c.dom.*;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 *
 * @author Yeeku.H.Lee kongyeeku@163.com
 * @version 1.0
 */
public class DTDParse {
    public static void main(String[] args) throws Exception {
        //DOM解析器工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        //获取DOM解析器
        DocumentBuilder builder = factory.newDocumentBuilder();
        //解析XML文档，并获取该XML文档对应的Document
        Document doc = builder.parse(new ClassPathResource("ch10_02/book2.xml").getFile());
        //获取该文档的DocumentType对象
        DocumentType docType = doc.getDoctype();
        System.out.println("根元素为：" + docType.getName());
        System.out.println("DTD的系统URI:" + docType.getSystemId());
        System.out.println("DTD的内部子集为:\n" + docType.getInternalSubset());
        //获取该DTD中包含的所有实体
        NamedNodeMap entities = docType.getEntities();
        //遍历DTD中的所有实体
        for (int i = 0; i < entities.getLength(); i++) {
            System.out.println("----------第" + i + "个实体的信息----------");
            Entity entity = (Entity) entities.item(i);
            System.out.println("实体名：" + entity.getNodeName());
            System.out.println("实体对应的符号名称：" + entity.getNotationName());
            System.out.println("实体的公共URI：" + entity.getPublicId());
            System.out.println("实体的系统URI：" + entity.getSystemId());
        }
        //获取该DTD中包含的所有符号
        NamedNodeMap notations = docType.getNotations();
        //遍历DTD中的所有符号
        for (int i = 0; i < notations.getLength(); i++) {
            System.out.println("----------第" + i + "个符号的信息----------");
            Notation notation = (Notation) notations.item(i);
            System.out.println("符号名称：" + notation.getNodeName());
            System.out.println("符号的公共URI：" + notation.getPublicId());
            System.out.println("符号的系统URI：" + notation.getSystemId());
        }
    }
}
